//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.entity.Clubmember;
import com.example.entity.President;
import com.example.entity.Recruitmentapplication;
import com.example.mapper.ClubmemberMapper;
import com.example.mapper.PresidentMapper;
import com.example.mapper.RecruitmentapplicationMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class RecruitmentapplicationService {
    @Resource
    private RecruitmentapplicationMapper recruitmentapplicationMapper;
    @Resource
    private PresidentMapper presidentMapper;

    @Resource
    private ClubmemberMapper clubmemberMapper;
    public RecruitmentapplicationService() {
    }

    public void add(Recruitmentapplication recruitmentapplication) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(new Date());
        recruitmentapplication.setApplicationTime(formattedDate);
        President president = presidentMapper.selectByClubId(recruitmentapplication.getClubId());
        recruitmentapplication.setPresidentId(president.getId());
        this.recruitmentapplicationMapper.insert(recruitmentapplication);
    }

    @Transactional
    public void updateById(Recruitmentapplication recruitmentapplication) {
        this.recruitmentapplicationMapper.updateById(recruitmentapplication);
        //同时把他加入到社团成员表中
        President president = presidentMapper.selectById(recruitmentapplication.getPresidentId());
        Integer clubId = president.getClubId();
        Clubmember clubmember = new Clubmember();
        clubmember.setClubId(clubId);
        clubmember.setStudentId(recruitmentapplication.getStudentId());
        clubmember.setPresidentId(recruitmentapplication.getPresidentId());
        clubmember.setRole("member");
        clubmemberMapper.insert(clubmember);
    }

    public void deleteById(Integer id) {
        this.recruitmentapplicationMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        Iterator var2 = ids.iterator();

        while(var2.hasNext()) {
            Integer id = (Integer)var2.next();
            this.deleteById(id);
        }

    }

    public Recruitmentapplication selectById(Integer id) {
        return this.recruitmentapplicationMapper.selectById(id);
    }

    public List<Recruitmentapplication> selectAll(Recruitmentapplication recruitmentapplication) {
        return this.recruitmentapplicationMapper.selectAll(recruitmentapplication);
    }

    public List<Recruitmentapplication> selectList(Recruitmentapplication recruitmentapplication) {
        System.out.println(recruitmentapplication);
        return null;
    }

    public PageInfo<Recruitmentapplication> selectPage(Recruitmentapplication recruitmentapplication, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Recruitmentapplication> list = this.recruitmentapplicationMapper.selectAll(recruitmentapplication);
        return PageInfo.of(list);
    }

    public List<Recruitmentapplication> selectByRecruitmentId(Integer recruitmentId) {
        return recruitmentapplicationMapper.selectByRecruitmetId(recruitmentId);
    }
}
